package game

import model.Card
import model.ActionCard
import model.TreasureCard
import java.util.ArrayList
import util.safeAdd
import state.ActionState
import model.Deck
import model.OpenDeck

enum class PlayerState { WAITING ACTING BUYING }

class Player(val name: String) {

    val deck = Deck.startDeck()
    val hand = ArrayList<Card>()
    val discard = OpenDeck()
    val playingArea = ArrayList<Card>()

    var turns = 0
    var actions = 0
    var buys = 0
    var coins = 0

    {
        5 times { hand.safeAdd(deck.pop()) }
    }

    fun toString() = name

    fun nextCard(): Card? {
        if (deck.size() > 0) return deck.pop()
        discard.forEach { deck.push(it) }
        discard.clear()
        deck.shuffle()
        return deck.pop()
    }

    fun amountAvailable() = hand.fold(coins, { sum, card -> sum + if (card is TreasureCard) card.coins else 0 })

    var menu: Menu? = null
    var state = PlayerState.WAITING
    var actionState: ActionState? = null
    fun getCurrentMenu() = menu

    fun start() {
        state = PlayerState.ACTING
        menu = Menu("Select card to act: ", hand.filter { it is ActionCard }, true)
    }


}
